﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Redmine.Core.Entities;
using Redmine.Core.Repositories;
using UnitOfWork;
using Redmine.Core.Extentions;
using UnitOfWork.Validation;

namespace Redmine.Web.Controllers
{
    public class UserController : BaseController
    {
        private readonly IUserRepository _userRepository;

        public UserController(IUserRepository userRepository, IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            _userRepository = userRepository;
        }

        //
        // GET: /User/

        public ViewResult Index()
        {
            return View(_userRepository.FindAll().ToList());
        }

        //
        // GET: /User/Details/5

        public ViewResult Details(int id)
        {
            User user = _userRepository.FindById(id);
            return View(user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                _userRepository.Add(user);
                _unitOfWork.Commit();
                return RedirectToAction("Index");
                //  List<ValidationResult> validationResults;
                //                if ()
                //                {
                //                   
                //                }
                //                else
                //                    ModelState.AddModelErrors(validationResults);

            }

            return View(user);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            User user = _userRepository.FindById(id);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                _userRepository.Update(user);
                _unitOfWork.Commit();
                return RedirectToAction("Index");
            }
            return View(user);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            User user = _userRepository.FindById(id);
            return View(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            User user = _userRepository.FindById(id);
            _userRepository.Delete(user);
            _unitOfWork.Commit();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            //db.Dispose();
            base.Dispose(disposing);
        }
    }
}